package com.huawei.secure.android.common.encrypt.rsa;

import android.text.TextUtils;
import android.util.Base64;
import android.util.Log;
import com.huawei.secure.android.common.encrypt.utils.EncryptUtil;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

public abstract class RSASign {
    private static final String CHARSET = "UTF-8";
    private static final String TAG = "RSASign";
    private static final String i = "";
    private static final String s = "SHA256WithRSA";

    public static String sign(String str, String str2) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str2)) {
            return sign(str, EncryptUtil.getPrivateKey(str2));
        }
        Log.e(TAG, "sign content or key is null");
        return "";
    }

    public static String sign(String str, PrivateKey privateKey) {
        Exception e;
        if (TextUtils.isEmpty(str) || privateKey == null || !RSAEncrypt.isPrivateKeyLengthRight((RSAPrivateKey) privateKey)) {
            Log.e(TAG, "content or privateKey is null , or length is too short");
            return "";
        }
        try {
            Signature instance = Signature.getInstance(s);
            instance.initSign(privateKey);
            instance.update(str.getBytes(CHARSET));
            return Base64.encodeToString(instance.sign(), 0);
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "sign exception: " + e.getMessage());
            return "";
        } catch (UnsupportedEncodingException e3) {
            e = e3;
            Log.e(TAG, "sign exception: " + e.getMessage());
            return "";
        }
    }

    public static boolean verifySign(String str, String str2, String str3) {
        if (!TextUtils.isEmpty(str) && !TextUtils.isEmpty(str3) && !TextUtils.isEmpty(str2)) {
            return verifySign(str, str2, EncryptUtil.getPublicKey(str3));
        }
        Log.e(TAG, "content or public key or sign value is null");
        return false;
    }

    public static boolean verifySign(String str, String str2, PublicKey publicKey) {
        Exception e;
        if (TextUtils.isEmpty(str) || publicKey == null || TextUtils.isEmpty(str2) || !RSAEncrypt.isPublicKeyLengthRight((RSAPublicKey) publicKey)) {
            Log.e(TAG, "content or publicKey is null , or length is too short");
            return false;
        }
        try {
            Signature instance = Signature.getInstance(s);
            instance.initVerify(publicKey);
            instance.update(str.getBytes(CHARSET));
            return instance.verify(Base64.decode(str2, 0));
        } catch (GeneralSecurityException e2) {
            e = e2;
            Log.e(TAG, "check sign exception: " + e.getMessage());
            return false;
        } catch (UnsupportedEncodingException e3) {
            e = e3;
            Log.e(TAG, "check sign exception: " + e.getMessage());
            return false;
        }
    }
}
